package com.example.rabbitmq.direct.service;

import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * @Auther:
 * @Description:
 * @Date: 2019/12/26 17:46
 */
@Service
public class DirectSendMessageService {
    @Autowired
    private RabbitTemplate rabbitTemplate;

    public String sendMsg(String msg) {
        try {
            msg += LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
            System.out.println("Direct Exchange 服务端发送的消息信息是：  " + msg);
            rabbitTemplate.convertAndSend("myDirectExchange", "myDirectRoutingKey", msg);
        } catch (Exception e) {
            e.printStackTrace();
            return "send msg fail";
        }
        return "send msg success";
    }
}
